package com.person.mall.controller;

import com.person.mall.consts.MallConst;
import com.person.mall.form.CartAddForm;
import com.person.mall.form.CartUpdateForm;
import com.person.mall.pojo.User;
import com.person.mall.service.ICartService;
import com.person.mall.vo.CartVo;
import com.person.mall.vo.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * @author 最酷的桃儿wjc
 * * @date 2022/5/19
 */
@RestController
public class CartController {
    @Autowired
    private ICartService cartService;

    /**
     * 添加购物车
     * @param cartAddForm
     * @param session
     * @return
     */
    @PostMapping("/carts")
    public ResponseVo<CartVo> addCart(@Valid @RequestBody CartAddForm cartAddForm, HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.addCart(user.getId(),cartAddForm);
    }

    /**
     * 获取购物车列表
     * @param session
     * @return
     */
    @GetMapping("/carts")
    public ResponseVo<CartVo> cartList(HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.cartList(user.getId());
    }

    /**
     * 更新购物车
     * @param productId
     * @param cartUpdateForm
     * @param session
     * @return
     */

    @PutMapping("/carts/{productId}")
    public ResponseVo<CartVo> updateCart(@PathVariable Integer productId, @Valid @RequestBody CartUpdateForm cartUpdateForm, HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.update(user.getId(),productId,cartUpdateForm);
    }

    /**
     * 删除购物车
     * @param productId
     * @param session
     * @return
     */
    @DeleteMapping("/carts/{productId}")
    public ResponseVo<CartVo> deleteCart(@PathVariable Integer productId,HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.delete(user.getId(),productId);
    }

    /**
     * 购物车全选
     * @param session
     * @return
     */
    @PutMapping("/carts/selectAll")
    public ResponseVo<CartVo> selectAll(HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.selectAll(user.getId());
    }

    /**
     * 购物车全不选
     * @param session
     * @return
     */
    @PutMapping("/carts/unselectAll")
    public ResponseVo<CartVo> unselectAll(HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.unselectAll(user.getId());
    }

    /**
     * 获取购物车的总数量
     * @param session
     * @return
     */
    @GetMapping("/carts/products/sum")
    public ResponseVo<Integer> sunm(HttpSession session){
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.sumData(user.getId());
    }
}
